package org.elanfox.intellicommunity.intellicommunityadmin.ws;

import lombok.extern.slf4j.Slf4j;
import org.elanfox.intellicommunity.biz.dto.msg.AlarmMsgDTO;
import org.elanfox.intellicommunity.biz.dto.msg.MsgBaseDTO;
import org.elanfox.intellicommunity.biz.entity.equipment.CardPO;
import org.elanfox.intellicommunity.biz.entity.equipment.LocatorPO;
import org.elanfox.intellicommunity.biz.service.core.equipment.CardService;
import org.elanfox.intellicommunity.biz.service.core.msg.AlarmMsgService;
import org.elanfox.intellicommunity.common.ienum.CardEnum;
import org.elanfox.intellicommunity.common.ienum.MsgEnums;
import org.elanfox.intellicommunity.msg.dto.RawTagDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Service;

/**
 * @author allan
 * @version 1.0.0
 * @date 2019/12/24
 */
@Service
@Slf4j
public class WsMsgHandler {
    @Autowired(required = false)
    private SimpMessagingTemplate simpMessagingTemplate;
    @Autowired
    private AlarmMsgService alarmMsgService;
    @Autowired
    private CardService cardService;

    public void send(MsgBaseDTO message) {
        try {
            simpMessagingTemplate.convertAndSend("/topic/alarm", message);
        } catch (Exception ex) {
            log.error("websocket sendMessage exception-->" + ex.getMessage(), ex);
        }
    }

    public void send(Object object) {
        try {
            simpMessagingTemplate.convertAndSend("/topic/alarm", object);
        } catch (Exception ex) {
            log.error("websocket sendMessage exception-->" + ex.getMessage(), ex);
        }
    }

    public void send(CardPO cardPO, RawTagDTO rawMsgDTO, String alarmTemplate) {
        AlarmMsgDTO lastAlarm = alarmMsgService.findLastByUser(cardPO.getUser().getUserId());
        if (lastAlarm == null || lastAlarm.getAlarmStatus() < MsgEnums.AlarmStatus.ON_LOAD.getCode()) {
            AlarmMsgDTO alarmMsgDTO = new AlarmMsgDTO();
            alarmMsgDTO.setUserId(cardPO.getUser().getUserId());
            alarmMsgDTO.setRealName(cardPO.getUser().getName());
            alarmMsgDTO.setUserType(cardPO.getUser().getUserType().getCode());
            alarmMsgDTO.setAlarmTemplate(alarmTemplate);
            alarmMsgDTO.setRoomNo(cardPO.getUser().getRoomNumber());

            if (rawMsgDTO.isBatteryNotEnough()) {
                alarmMsgDTO.setAlarmType(MsgEnums.AlarmType.BATTERY_ALARM);

                //this.send(alarmMsgDTO);

                //设置卡为低电压
                if (cardPO.getVoltageStatus() == CardEnum.VoltageStatusEnum.STATUS_0) {
                    log.info("低电压设置前========"+cardPO.getCardNo()+";"+cardPO.getVoltageStatus().getDesc());
                    cardService.updateVoltageStatus(cardPO.getCardNo(), CardEnum.VoltageStatusEnum.STATUS_1);
                    log.info("低电压设置后========"+cardPO.getCardNo()+";"+cardPO.getVoltageStatus().getDesc());
                }
            } else {
                if (cardPO.getVoltageStatus() == CardEnum.VoltageStatusEnum.STATUS_1) {
                    //重新设置成电压正常
                    cardService.updateVoltageStatus(cardPO.getCardNo(), CardEnum.VoltageStatusEnum.STATUS_0);
                }
            }
            if (rawMsgDTO.isAlarmedButton()) {
                alarmMsgDTO.setAlarmType(MsgEnums.AlarmType.ALARM_BUTTON);
                this.send(alarmMsgDTO);
            }
        }
    }

    public void send(CardPO cardPO, LocatorPO locatorPO) {
        AlarmMsgDTO lastAlarm = alarmMsgService.findLastByUser(cardPO.getUser().getUserId());
        if (lastAlarm == null || lastAlarm.getAlarmStatus() < MsgEnums.AlarmStatus.ON_LOAD.getCode()) {
            AlarmMsgDTO alarmMsgDTO = new AlarmMsgDTO();
            alarmMsgDTO.setUserId(cardPO.getUser().getUserId());
            alarmMsgDTO.setRealName(cardPO.getUser().getName());
            alarmMsgDTO.setUserType(cardPO.getUser().getUserType().getCode());
            alarmMsgDTO.setAlarmTemplate(locatorPO.getAlarmTemplate());
            alarmMsgDTO.setAlarmType(MsgEnums.AlarmType.SPECIAL_ALARM);
            alarmMsgDTO.setRoomNo(cardPO.getUser().getRoomNumber());
            this.send(alarmMsgDTO);
        }
    }
}
